﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProjectStore30032013.PRESENTATION;
using ProjectStore30032013.DAL;

namespace ProjectStore30032013.PRESENTATION
{
    public partial class cmbUnitPrice : Form
    {
        public cmbUnitPrice()
        {
            InitializeComponent();
        }
        public void displayIsEdit(string id_pro){
            m_e_form = e_form_mode.SUA_SAN_PHAM;            
            m_product = (from d in m_dtct_product.Products
                         where d.ProductID == id_pro
                         select d).Single();

        }
        private void btnSave_Click(object sender, EventArgs e)
        {

            try
            {
                switch (m_e_form)
                {
                    case e_form_mode.THEM_SAN_PHAM:
                        insert_obj_to_database();
                        MessageBox.Show("A new product has been added successfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        break;
                    case e_form_mode.SUA_SAN_PHAM:
                        update_obj_to_database();
                        MessageBox.Show("This product's information has been updated successfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        break;
                    case e_form_mode.XEM_SAN_PHAM:
                        break;
                    default:
                        break;
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            

            txtProductID.Text = "";
            txtProductName.Text = "";
            txtProductNote.Text = "";
            txtProductVAT.Text = "";
            cmbProductUnit.Text = "";
            cmbProductCatelogy.Text = "";
        }

        private void frmProductAdd_Load(object sender, EventArgs e)
        {
            set_form_load();
        }

        #region Members
        Product m_product;
        DataCSQLDataContext m_dtct_product = new DataCSQLDataContext(new Controler().ConnectionString);
        e_form_mode m_e_form = e_form_mode.THEM_SAN_PHAM;
        #endregion

        #region Data Structures
        enum e_form_mode
        {
            THEM_SAN_PHAM = 1,
            SUA_SAN_PHAM = 2,
            XEM_SAN_PHAM = 3
        }
        #endregion

        #region Private Methods
        //Gán thông tin từ form vào object
        private void form_to_obj()
        {            
            m_product.ProductID = txtProductID.Text;
            m_product.ProductName = txtProductName.Text;
            m_product.RateVAT = Convert.ToInt32(txtProductVAT.Text);
            m_product.Unit = cmbProductUnit.SelectedValue.ToString();
            m_product.CategoryID = cmbProductCatelogy.SelectedValue.ToString();
            m_product.Description = txtProductNote.Text;
            m_product.Price = Convert.ToDecimal(txtProductPrice.Text);
         

        }

        //Gán thông tin từ object lên form
        private void obj_to_form()
        {
            txtProductID.Text = m_product.ProductID;
            txtProductName.Text = m_product.ProductName;
            txtProductVAT.Text = m_product.RateVAT.ToString() ;
            txtProductNote.Text = m_product.Description;
            cmbProductUnit.SelectedValue = m_product.Unit;
   

        }

        private void insert_obj_to_database()
        {

            form_to_obj();
            m_dtct_product.Products.InsertOnSubmit(m_product);
            m_dtct_product.SubmitChanges();
        }

        private void update_obj_to_database()
        {
            form_to_obj();
            //m_dtct_product.Products.InsertOnSubmit(m_product);
            m_dtct_product.SubmitChanges();
        }

        private void lam_lai_obj_to_database()
        {

        }

        private void set_form_load()
        {

            DataCSQLDataContext m_combo = new DataCSQLDataContext(new Controler().ConnectionString);
            var k = from d in m_combo.Units
                    select new { d.UnitId, d.UnitName };
            cmbProductUnit.DataSource = k;
            cmbProductUnit.DisplayMember = "UnitName";
            cmbProductUnit.ValueMember = "UnitId";

            DataCSQLDataContext m_combo2 = new DataCSQLDataContext(new Controler().ConnectionString);
            var l = from d in m_combo2.Categories
                    select new { d.CategoryID, d.CategoryName };
            cmbProductCatelogy.DataSource = l;
            cmbProductCatelogy.DisplayMember = "CategoryName";
            cmbProductCatelogy.ValueMember = "CategoryID";


            DataCSQLDataContext dataContext = new DataCSQLDataContext(new Controler().ConnectionString);
            var t = from d in dataContext.UnitMoneys
                    select new {d.UnitMoneyID, d.UnitMoneyName};
            this.cmbUnitPriceProduct.DataSource = t;
            this.cmbUnitPriceProduct.DisplayMember = "UnitMoneyName";
            this.cmbUnitPriceProduct.ValueMember = "UnitMoneyID"; 

            if (m_e_form == e_form_mode.SUA_SAN_PHAM) txtProductID.Enabled = false; else txtProductID.Enabled = true;
            
            switch (m_e_form)
            {
                case e_form_mode.THEM_SAN_PHAM:
                    m_product = new Product();                    
                    break;
                case e_form_mode.SUA_SAN_PHAM:
                    obj_to_form();
                    break;
                case e_form_mode.XEM_SAN_PHAM:
                    btn_them.Visible = false;
                    break;
                default:
                    break;
            }
           
        }
        #endregion

        #region Events
        private void set_form_event()
        {
            btn_thoat.Click += new EventHandler(btn_thoat_Click);
            this.KeyDown += new KeyEventHandler(frm_cap_nhat_hang_ban_KeyDown);
            btn_them.Click += new EventHandler(btnSave_Click);
        }



        void frm_cap_nhat_hang_ban_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                switch (e.KeyCode)
                {
                    case Keys.Escape:
                        this.Close();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_cap_nhat_hang_ban_Load(object sender, EventArgs e)
        {
            set_form_load();
        }
        #endregion

        private void btn_thoat_Click_1(object sender, EventArgs e)
        {
            frmManageProduct a = new frmManageProduct();
            a.load();
            this.Close();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}
